<template>
  <!-- 上传厂家证照的表格modal -->
  <div>
    <basic-modal
      :modalOpt="modal"
      :buttonText="['','关闭']"
      @onMakeSure="onPush"
      @closeModal="closeModal"
      :footer="footer"
    >
      <template #cusBtn>
        <a-button
          type="primary"
          v-print="printObj"
        > 打印 </a-button>
      </template>
      <template>
        <div style="display:none">
          <print-html
            ref="print"
            id="print"
            :purOrderPid="pid"
            :infoDatas="infoDatas"
            :BarCode="BarCode"
            :columns="columns"
          ></print-html>
        </div>
        <div id="viewTest">
          <batch-operat-btn
            v-if="editable"
            :isView="false"
            :addData="true"
            @onAddData="onAddData"
          >
            <template #cusBtn> </template>
          </batch-operat-btn>
          <a-list-info
            :infoDatas="infoDatas"
            :haveBarCode="true"
            :BarCode="BarCode"
          />
          <div class="recordBox">
            <a-list-table
              :columns="columns"
              :tableData="listdata"
              :isSelecttion="false"
              :loading="loading"
              :renderStateByField="['purOrderState']"
              :scroll="{ y: null }"
              :pagination="pagination"
              @onTableChange="handleTableChange"
            >
            </a-list-table>
          </div>
        </div>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import aListTable from '@/views/app/biz/common/aListTable'
import aListInfo from '@/views/app/biz/common/aListInfo'
import batchOperatBtn from '@/views/app/biz/common/batchOperatBtn'
import printHtml from './printHtml.vue'
import storage from 'store'
import {
  getInfoByPid,
  getDetailByConditions,
  getDetailByConditionsCount
} from './purchaseOrder'

export default {
  components: {
    basicModal,
    aListTable,
    batchOperatBtn,
    aListInfo,
    printHtml
  },
  data () {
    return {
      printObj: {
        id: "print",
        preview: false,//是否开启打印预览
        previewTitle: '打印预览', // The title of the preview window. The default is 打印预览
        popTitle: '采购单',
        extraCss: "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",
        extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
        previewBeforeOpenCallback (vue) {
          console.log('正在加载预览窗口')
        },
        previewOpenCallback (vue) {
          console.log('已经加载完预览窗口')
        },
        beforeOpenCallback (vue) {
          vue.printLoading = true
          console.log('打开之前')
        },
        openCallback (vue) {
          vue.printLoading = false
          console.log('执行了打印')
        },
        closeCallback (vue) {
          console.log('关闭了打印工具')
        }
      },
      footer: undefined,
      editable: false,
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '审核证照',
        modalWidth: '80vw',
        maxHeight: '70vh'
      },
      infoDatas: [
        { title: '采购单号', id: 'purOrderNo', value: '' },
        { title: '采购单名称', id: 'purOrderName', value: '' },
        { title: '采购类型', id: 'purType', value: '' },
        { title: '采购单位备注', id: 'remarks', value: '' },
        { title: '无税总价', id: 'totalIunitprice', value: '', style: 'fontWeight:bolder', toFixed2: true },
        { title: '含税总价', id: 'totalPrice', value: '', style: 'fontWeight:bolder', toFixed2: true },
        { title: '订单状态', id: 'purOrderState', value: '' },
        { title: '相关联订单号', id: 'relationPurOrderNo', value: '' },
        { title: '送货类型', id: 'deliveryType', value: '' },
        { title: '收货地址', id: 'deliveryAddr', value: '' },
        { title: '收货人', id: 'delivery', value: '' },
        { title: '收货人电话', id: 'deliveryTel', value: '' },
        // { title: '收货人及电话', id: 'consignee', value: '' },
        { title: '配送状态', id: 'deliveryState', value: '' },
        { title: '开票状态', id: 'invoiceState', value: '' },
        { title: '采购单生成时间', id: 'purOrderCreateTime', value: '' },
        { title: '采购单确认时间', id: 'purOrderCfmTime', value: '' },
        { title: '采购单完成时间', id: 'purOrderFinTime', value: '' },
      ],
      columns: [
        { title: '编码', dataIndex: 'standardCode', key: 'standardCode', align: 'center' },
        { title: '品名', dataIndex: 'productNameZh', key: 'productNameZh', align: 'center', },
        { title: '供应商名称', dataIndex: 'supplierName', key: 'supplierName', align: 'center' },
        // { title: '通用名', dataIndex: 'commonName', key: 'commonName', align: 'center' },
        { title: '订单号', dataIndex: 'purOrderNo', key: 'purOrderNo', align: 'center', },
        { title: '生产厂家名称', dataIndex: 'manufacturerZh', key: 'manufacturerZh', align: 'center', },
        { title: '规格', dataIndex: 'spec', key: 'spec', align: 'center' },
        // { title: '型号', dataIndex: 'model', key: 'model', align: 'center' },
        // { title: '批号', dataIndex: 'batchCode', key: 'batchCode', align: 'center',width:'150px',scopedSlots: { customRender: 'batchCode' } },
        // { title: '效期', dataIndex: 'expiryDate', key: 'expiryDate', align: 'center' },
        { title: '配送数量', dataIndex: 'num', key: 'num', align: 'center', width: '100px', scopedSlots: { customRender: 'num' } },
        // { title: '备货数量', dataIndex: 'readyNum', key: 'readyNum', align: 'center',width:'100px',scopedSlots: { customRender: 'readyNum' } },
        { title: '单品含税价', dataIndex: 'iunitprice', key: 'iunitprice', align: 'center', scopedSlots: { customRender: 'tofixed2' } },
        { title: '总价', dataIndex: 'totalPrice', key: 'totalPrice', align: 'center', scopedSlots: { customRender: 'tofixed2' } },
        { title: '状态', dataIndex: 'purOrderState', key: 'purOrderState', align: 'center', scopedSlots: { customRender: 'purOrderState' } },
      ],
      sData: {
        purOrderPid: null,
      },
      listdata: [],
      BarCode: null,
      pagination: {
        //分页配置
        current: 1, //当前页码 current
        pageSize: 10, //每页显示条数 pageSize
        total: 1,
        showQuickJumper: true,
        showSizeChanger: true,
        pageSizeOptions: ['1', '5', '10', '100'],
      },
      listKey: 0,
      pid: '',
      zzPid: '',
      sysSeq: 0,
      loadings: false,
    }
  },
  computed: {},
  mounted () { },
  methods: {
    onPrint () {
      this.$print(this.$refs.print)
    },
    // // 弹出模态框
    showModal (val, record) {
      this.pid = val
      this.record = record
      this.modal.modalVisible = true
      this.modal.modalTitle = '查看详情'
      this.sData.purOrderPid = val
      this.getSBypid(val)
      this.getTotal()
      this.getDetailBypid(this.sData)

    },
    // 关闭模态详细框
    closeModal () {
      this.modal.modalVisible = false
      this.$emit('close')
      Object.assign(this.$data, this.$options.data())
    },
    closeModal2 () {
      this.modal.modalVisible = false
      this.$emit('ok')
      Object.assign(this.$data, this.$options.data())
    },
    //
    onAddData () {
      this.$refs.addSellerLicChildModal.addModal(this.pid)
    },

    // 审核
    onPush () {
    },

    // 得到销售订单消息
    async getSBypid (val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        console.log(res.data);
        this.sysSeq = res.data.sysSeq
        this.BarCode = res.data.purOrderNo
        this.printObj.popTitle = '采购单号：' + res.data.purOrderNo + ' , 采购单位：' + storage.get('orgName')
        for (const key in res.data) {
          for (const item of this.infoDatas) {
            if (key == item.id) {
              item.value = res.data[key]
            }
          }
        }
      }
    },
    // 触发改变页码,筛选,排序时的回调
    handleTableChange (pagination, filters, sorter) {
      this.pagination.current = pagination.current
      this.pagination.pageSize = pagination.pageSize
      this.getDetailBypid(this.sData)
    },
    // 初始化页面时得到总数
    async getTotal () {
      let pages = ''
      pages = await getDetailByConditionsCount(this.sData)
      console.log(pages)
      if (pages && pages.code && pages.code === 'C1') {
        const pagination = { ...this.pagination }
        pagination.total = pages.data
        this.pagination = pagination
        this.pagination.showTotal = (showTotal) => { return `共 ${showTotal} 条` }
      }
    },
    //得到初始化订单商品list
    async getDetailBypid (params) {
      console.log('请求列表时传入的数据', params)
      this.loading = true
      let listdata = []
      let res = ''
      res = await getDetailByConditions(params, this.pagination)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        const resdata = res.data
        this.orgList = res.data
        for (let item of resdata) {
          listdata.push({
            key: item.pid,
            ...item
          }
          )
        }
        this.listdata = listdata
      } else {
        this.listdata = []
      }
    },
  },
}
</script>

<style>
.recordBox {
  width: 100%;
  min-height: 80px;
}
</style>